package com.great.controller;

import com.github.pagehelper.PageInfo;
import com.great.domain.Manage;
import com.great.domain.User;
import com.great.service.ManageService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.SessionAttributes;
import org.springframework.web.bind.support.SessionStatus;
import org.springframework.web.servlet.ModelAndView;

import java.util.List;

@SessionAttributes(value = {"manage"})
@Controller
@RequestMapping("/Manage")
public class ManageController {

    @Autowired
    private ManageService manageService;


   @RequestMapping("/loginin")
   public String loginin(){
       return "manage/manageLogin";
   }


    @RequestMapping("/login")
    public String login(Model model, String manage_Name, String manage_Password) {
        int count = manageService.managenamecheck(manage_Name);
        if (count > 0) {
            Manage manage = manageService.selectbyname(manage_Name, manage_Password);
            if (manage == null) {
                model.addAttribute("errMsg", "密码错误");
                return "manage/manageLogin";
            } else {
                model.addAttribute("manage", manage);
                return "redirect:/pages/manage/main.jsp";
            }
        } else {
            model.addAttribute("errMsg", "用户名不存在");
            return "manage/manageLogin";
        }
    }





    @RequestMapping("/searchmanage")
    public ModelAndView searchmanage(@RequestParam(name = "page", required = true,defaultValue = "1") int page, @RequestParam(name = "size",required = true,defaultValue = "5") int size,
                                   String keyword){
        ModelAndView mv = new ModelAndView();
        List<Manage> manages = manageService.searchmanage(page, size,keyword);
        PageInfo pageInfo = new PageInfo(manages);
        mv.addObject("pageInfo",pageInfo);
        mv.setViewName("manage/manage-list");
        return mv;
    }



    @RequestMapping("/delmanagemore")
    public String delmanagemore(String tag){
        String[] strs = tag.split(",");
        for(int i=0;i<strs.length;i++){
            manageService.delmanage(strs[i]);
        }
        return "redirect:/Manage/searchmanage";
    }


    @RequestMapping("/addmanage")
    public String addmanage(Manage manage){
        manageService.addmanage(manage);
        return "redirect:/Manage/searchmanage";
    }



    @RequestMapping("/findmanageById")
    public ModelAndView findmanageById(String id){
        ModelAndView mv = new ModelAndView();
        Manage manage = manageService.finById(id);
        mv.addObject("manage",manage);
        mv.setViewName("manage/manage-change");
        return mv;
    }

    @RequestMapping("/updateById")
    public String updateById(Manage manage){
        manageService.updateById(manage);
        return "redirect:/Manage/searchmanage.jsp";
    }

    @RequestMapping("/Logout")
    public String Logout(SessionStatus sessionStatus){
        sessionStatus.setComplete();
        return "redirect:/pages/manage/manageLogin.jsp";
    }

}
